package cn.dinghu.pro.common;

import java.io.Serializable;

/**
 * @author 熙宝
 * @create 2025-05-21-11:36
 */
public class ResultData<T> implements Serializable {
    private int code;
    private String msg;
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 请求成功
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> ok() {
        return restResult(null,HttpEnum.OK_200.code(), HttpEnum.OK_200.desc());
    }

    /**
     * 请求成功 返回数据
     * @param t
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> ok(T t) {
        return restResult(t,HttpEnum.OK_200.code(), HttpEnum.OK_200.desc());
    }

    /**
     * 有响应体 有响应信息
     * @param t
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> ok(T t,String msg) {
        return restResult(t,HttpEnum.OK_200.code(), msg);
    }

    /**
     * 没有获取到有效数据
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> noData() {
        return restResult(null,HttpEnum.FAIL_200.code(), "没有获取到相应的数据信息");
    }

    /**
     * 500错误
      * @return
     * @param <T>
     */
    public static <T> ResultData<T> error(){
        return restResult(null,HttpEnum.ERROR_500.code(), HttpEnum.ERROR_500.desc());
    }

    /**
     * 500错误
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> error(String msg){
        return restResult(null,HttpEnum.ERROR_500.code(), msg);
    }

    /**
     * 500 错误
     * @param code
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> error(int code,String msg){
        return restResult(null,code, msg);
    }

    /**
     * 502 请求失败
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> FAIL_502(){
        return restResult(null,HttpEnum.FAIL_502.code(), HttpEnum.FAIL_502.desc());
    }


    /**
     * 600非法参数
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> error_600(){
        return restResult(null,HttpEnum.ERROR_600.code(), HttpEnum.ERROR_600.desc());
    }


    /**
     * 404 资源不存在
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> notFound_404(){
        return restResult(null,HttpEnum.NOTFOUND_404.code(), HttpEnum.NOTFOUND_404.desc());
    }


    /**
     * 201操作成功
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> ok_201(){
        return restResult(null,HttpEnum.HADNLE_201.code(), HttpEnum.HADNLE_201.desc());
    }

    /**
     * 201操作失败
     * @return
     * @param <T>
     */
    public static <T> ResultData<T> fail_201(){
        return restResult(null,HttpEnum.HANDLE_500.code(), HttpEnum.HANDLE_500.desc());
    }

    private static <T> ResultData<T> restResult(T data,int code, String msg) {
        ResultData<T> apiResult = new ResultData<>();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        apiResult.setData(data);
        return apiResult;
    }
}
